#pragma GCC optimize(2)
#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
#include <stack>
#include <bitset>
#define F first
#define S second
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll mod=1e9+7;
void solve()
{
	int n,q;
	cin>>n>>q;
	vector<ll> k(n+1);
	vector<ll> b(n+1);
	vector<vector<ll>> f(31,vector<ll>(n+1,0));
	vector<vector<ll>> fk(31,vector<ll>(n+1,1));
	vector<vector<ll>> fb(31,vector<ll>(n+1,0));
	vector<int> p(n+1);
	for(int i=1;i<=n;i++) cin>>k[i];
	for(int i=1;i<=n;i++) cin>>b[i];
	for(int i=1;i<=n;i++) cin>>p[i];
	for(int z=0;z<=30;z++)
	{
		for(int i=1;i<=n;i++)
		{
			if(z==0)
			{
				f[z][i]=p[i];
				fk[z][i]=k[i];
				fb[z][i]=b[i];
			}
			else
			{
				f[z][i]=f[z-1][f[z-1][i]];
				fk[z][i]=fk[z-1][f[z-1][i]]*fk[z-1][i]%mod;
				fb[z][i]=(fb[z-1][f[z-1][i]]+fb[z-1][i]*fk[z-1][f[z-1][i]]%mod)%mod;
			}
		}
	}
	while(q--)
	{
		ll x,l,y;
		cin>>x>>l>>y;
		x=f[0][x];
		ll ans=y;
		for(int i=0;i<=30;i++)
		{
			if( ((l>>i)&1)==1 )
			{
				ans=ans*fk[i][x]%mod;
				ans=ans+fb[i][x]%mod;
				ans%=mod;
				x=f[i][x];
			}
		}
		cout<<ans<<'\n';
	}
	
}
int main()
{
	IOS;
	int T=1;
	cin>>T;

	while(T--)
	{
		solve();
	}

	return 0;
}

